package com.hrhx.duhongming.adapter.echarts;

import java.util.List;
import java.util.Map;

import com.github.abel533.echarts.Polar;
import com.github.abel533.echarts.code.SeriesType;
import com.github.abel533.echarts.code.Trigger;
import com.github.abel533.echarts.data.Data;
import com.github.abel533.echarts.json.GsonOption;
import com.github.abel533.echarts.series.Line;
import com.hrhx.duhongming.adapter.EchartsAdapter;
import com.hrhx.duhongming.bean.EchartsConfig;

public class EchartsRadarAdapter implements EchartsAdapter{
	
	public static final EchartsRadarAdapter me = new EchartsRadarAdapter();
	
	//radar的适配器入口
	public GsonOption adapter(
			EchartsConfig echartsConfig,
			GsonOption gsonOption,
			List<Map<String,Object>> data){
		//EchartsRadarAdapter radarAdapter = new EchartsRadarAdapter();
		gsonOption = me.legendAdapter(gsonOption, data);
		gsonOption = me.radarAdapter(gsonOption, data);
		gsonOption = me.seriesAdapter(echartsConfig,gsonOption, data);
		return gsonOption;
	}
		
	//legend
	public GsonOption legendAdapter(
			GsonOption gsonOption,
			List<Map<String,Object>> data){
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (String key : data.get(0).keySet()) {
				//图例组件：Y轴数据类型
				if(key.startsWith("y:")){
					gsonOption.legend().data(key.replace("y:", ""));
				}
			}
		}
		return gsonOption;
	}
	
	public GsonOption radarAdapter(GsonOption gsonOption,
			List<Map<String,Object>> data){
		Polar polar = new Polar();
		if(data != null && data.size() != 0){
			//获取第一个List中的Map中的值
			for (Map<String,Object> map:data) {
				//图例组件：Y轴数据类型
				polar.indicator(new Data().text(map.get("x:name").toString()).max(map.get("x:max")));
			}
		}
		gsonOption.polar(polar);
		return gsonOption;
	}
	//radius轴数据处理
	public GsonOption seriesAdapter(
			EchartsConfig echartsConfig,
			GsonOption gsonOption,
			List<Map<String,Object>> orientData){
		Line line = new Line();
		line.tooltip().trigger(Trigger.item);
		line.type(SeriesType.radar).name("预算 vs 开销（Budget vs spending）");
		if (orientData != null) {
			for(Object legendData : gsonOption.legend().data()){
				Data dataArr = new Data().name((String)legendData);
				Object[] obj = new Object[orientData.size()];
				for (int i=0;i<orientData.size();i++) {
					Map<String,Object> legendMap = orientData.get(i);
					obj[i] = legendMap.get("y:"+legendData);
					
				}
				dataArr.value(obj);
				line.data(dataArr);
			}
		}
		gsonOption.series(line);
		return gsonOption;
	}
}
